package com.dylearn.test;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

/**
 * @CreateBy zhangdingyi
 * @Date 2022-09-29
 * @Content 该模块主要是 学习 流的基本概念，明白什么是流
 */
@SpringBootTest
public class JavaCoreTechnologyVolume {

    //1.1 从迭代到流的操作
    @Test
    void test1() throws IOException {
        //System.out.println("\\PL+");
        Path path = Paths.get("D:\\java学习\\java基础\\java高级特性 --java核心技术卷2\\test.txt");
        System.out.println("path -> " + path);
        byte[] bytes = Files.readAllBytes(path);
        System.out.println("bytes -> " + bytes);
        for (byte aByte : bytes) {
            System.out.println("aByte -> " + aByte);
        }

        //读取文件
        String content = new String(bytes, StandardCharsets.UTF_8);
        System.out.println("content.split() = " + content.split("\\PL+"));
        List<String> world = Arrays.asList(content.split("\\PL+"));

        System.out.println("world -> " + world);
        long count = 0;
        //流操作
        for (String w : world) {
            if (w.length() > 1) count++;
        }
        System.out.println("count 1 -> " + count);

        count = world.stream().filter(w -> w.length() > 1).count();
        System.out.println("count 2 = " + count);

        count = world.parallelStream().filter(w -> w.length() > 1).count();
        System.out.println("count 3 = " + count);


    }

}
